package com.fileshare.web.service.impl;

import com.fileshare.web.dto.Bo.UploadFileBo;
import com.fileshare.web.dto.UploadItem;
import com.fileshare.web.dto.Vo.UploadFileVO;
import com.fileshare.web.entity.FileResource;
import com.fileshare.web.entity.User;
import com.fileshare.web.properties.AliOssProperties;
import com.fileshare.web.service.FileResourceService;
import com.fileshare.web.service.UploadService;
import com.fileshare.web.utils.AliOssUtil;
import com.fileshare.web.utils.SystemUtil;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.Objects;

import static com.fileshare.web.utils.AliOssUtil.getCustomFilePath;

@Service
public class UploadServiceImpl implements UploadService {
    @Resource
    private AliOssProperties aliOssProperties;
    @Resource
    private FileResourceService fileResourceService;

    @Override
    @SneakyThrows
    public UploadFileVO uploadFileFragment(HttpServletRequest request, UploadFileBo fileBo) {
        User user = SystemUtil.getLoginUser();
        UploadFileVO fileVO = UploadFileVO.builder().skipped(false).uploaded(false).build();
        if (request instanceof StandardMultipartHttpServletRequest) {
            StandardMultipartHttpServletRequest fileRequest = (StandardMultipartHttpServletRequest) request;
            try {
                Iterator<String> iterator = fileRequest.getFileNames();
                while (iterator.hasNext()) {
                    MultipartFile file = fileRequest.getFile(iterator.next());
                    long offset = (long) (fileBo.getChunkNumber() - 1) * fileBo.getChunkSize();
                    UploadItem item = new UploadItem();
                    item.setFragment(true);
                    item.setFileContent(Objects.requireNonNull(file).getInputStream());
                    item.setUploadName(getCustomFilePath(user, fileBo.getFilename()));
                    item.setOffset(offset);
                    uploadToAliOss(item);
                }
                fileVO.setUploaded(true);
                boolean uploadFinished = fileBo.getChunkNumber() >= fileBo.getTotalChunks();
                if (uploadFinished) {
                    String resourceUrl = String.format(
                            "http://%s.oss-cn-beijing.aliyuncs.com/%s", aliOssProperties.getBucket(),
                            getCustomFilePath(user, fileBo.getFilename())
                    );
                    FileResource resource = fileResourceService.addResource(fileBo, resourceUrl, user);
                    fileVO.setResourceUrl(resourceUrl);
                    fileVO.setResourceSeq(resource.getFileSeq());
                }
            } catch (Exception ignored) {
                fileVO.setUploaded(false);
            }
        }
        return fileVO;
    }

    @Override
    @SneakyThrows
    public String uploadFile(MultipartFile file) {
        User user = SystemUtil.getLoginUser();
        UploadItem item = new UploadItem();
        item.setUploadName(getCustomFilePath(user, Objects.requireNonNull(file.getOriginalFilename())));
        item.setOriginName(file.getOriginalFilename());
        item.setFragment(false);
        item.setFileContent(file.getInputStream());
        return AliOssUtil.uploadSingleFile(aliOssProperties, item, user);
    }

    private void uploadToAliOss(UploadItem item) {
        AliOssUtil.uploadFile(aliOssProperties, item);
    }

}
